package com.code.petapp.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.code.petapp.R;
import com.code.petapp.bean.UserBean;
import com.code.petapp.databinding.ActivityModPwdBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.UserUtils;

public class ModPwdActivity extends BaseActivity {
    private ActivityModPwdBinding binding;
    private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityModPwdBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        password = SPCommonUtils.getInstance().getPassword();
        binding.oldPwd.setText(password);
        setSave();
        binding.cancel.setOnClickListener(v -> finish());
    }
    private void setSave(){
        binding.save.setOnClickListener(v -> {
            String oldPassword = binding.oldPwd.getText().toString().trim();
            String newPassword = binding.newPwd.getText().toString().trim();
            String confirmPassword = binding.rePwd.getText().toString().trim();

            if (oldPassword.isEmpty()) {
                toastShortShow("旧密码不能为空");
                return;
            }
            if (newPassword.isEmpty()) {
                toastShortShow("新密码不能为空");
                return;
            }
            if (confirmPassword.isEmpty()) {
                toastShortShow("确认密码不能为空");
                return;
            }
            if (oldPassword.equals(newPassword)) {
                toastShortShow("新密码不能与旧密码相同");
                return;
            }
            if (!newPassword.equals(confirmPassword)) {
                toastShortShow("两次输入的密码不一致");
                return;
            }
            if (!oldPassword.equals(password)) {
                toastShortShow("旧密码错误");
                return;
            }
            UserBean userBean = new UserBean();
            userBean.setId(SPCommonUtils.getInstance().getUserId());
            userBean.setNewPassword(newPassword);
            userBean.setUsername(SPCommonUtils.getInstance().getUserName());
            userBean.setPassword(password);
            binding.save.setEnabled(false);
            UserUtils.updatePwd(SPCommonUtils.getInstance().getToken(), userBean, new HttpListener<>() {
                @Override
                public void success(String data) {
                    toastShortShow("密码修改成功，请重新登录");
                    SPCommonUtils.getInstance().Clear();
                    Intent intent = new Intent(ModPwdActivity.this, StartActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void error(int code, String error) {
                       toastShortShow(error);
                }
            });

        });
    }
}